import { ScrollView } from 'react-native-gesture-handler';
import { Button, List, WhiteSpace, Flex, Toast, Modal } from '@ant-design/react-native';
import Item from '@ant-design/react-native/lib/list/ListItem';
import React, { Component } from 'react';
import { StyleSheet, Text, View, Image, ToastAndroid, Alert } from 'react-native';
import { GetUserAddressInfo } from '~/api/owner';
import { setAddressState } from '~/api/public';

export default class MyAddress extends Component {
    static navigationOptions = {
        title: '我的地址',
    };

    constructor(props) {
        super(props);
        this.state = {
            itemname: '14725836999',
            itemnumber: '小明同学',
            itemaddress: '重庆市合川区华帝街领域32号',
            list: [],
            PuAddressID: '',
            id: '',
            address_id: '',
        };
        GetUserAddressInfo().then((r) => {
            if (r.code === 0) {
                this.setState({
                    list: r.data,
                });
            } else {
                Toast.info('失败', 1);
            }
        });
    }
    render() {
        const onDele = (PuAddressID) => {
            Modal.alert('提示', '删除成功', [{
                text: '确定', onPress: () => setAddressState({
                    address_id: PuAddressID,
                    state: 0,
                }).then((r) => {
                    if (r.code === 0) {
                        this.setState({
                            list: this.state.list.filter((item) => item.PuAddressID !=PuAddressID),
                        })
                    } else {
                        Toast.info('删除失败', 1);
                    }
                })
            }, { text: '取消' }]);
        };
        return (
            <ScrollView>
                <List>
                    {this.state.list.map((item, index) => {
                        return (
                            <Item
                                onPress={() => {
                                    this.props.navigation.push('AddAddress', {
                                        PuAddressID: item.PuAddressID,
                                    });
                                }}>
                                <View>
                                    <Flex style={{ flex: 1 }} justify={'between'}>
                                        <Flex direction={'row'} align={'center'} style={{ width: '80%' }}>
                                            <Text style={{ marginTop: 10, fontSize: 18 }}>{item.Contacter}</Text>
                                            <Text style={{ marginTop: 10, marginLeft: 15, fontSize: 12, color: '#9E9E9E' }}>{item.Telphone}</Text>
                                        </Flex>
                                        <Flex justify={'end'} align={'end'} direction={'row'} style={{ width: '20%' }}>
                                            {/* <Image style={{ width: 30, height: 30}} source={require('~/assets/my/xiugai.png')} /> */}
                                            <Button
                                                style={{ width: 70, height: 30, marginLeft: 20 }}
                                                type={'warning'}
                                                onPress={() => {
                                                    onDele(item.PuAddressID);
                                                }}>
                                                删除
                                        </Button>
                                        </Flex>

                                    </Flex>
                                    <Flex style={{ marginTop: 10, width: '100%' }} justify={'between'}>
                                        <Text style={{ width: '80%', color: '#9E9E9E' }}>
                                            地址：{item.CityName}
                                            {item.Address}
                                        </Text>
                                        {item.AddressState == 1 ? <Text style={{ color: 'red' }}>默认地址</Text> : null}
                                    </Flex>
                                </View>
                            </Item>
                        );
                    })}
                </List>
                <Flex justify="center">
                    <Button
                        style={{ marginTop: '10%', width: 200 }}
                        type="warning"
                        onPress={() => {
                            this.props.navigation.push('AddAddress', {
                                PuAddressID: 0,
                            });
                        }}>
                        新增地址+
                    </Button>
                </Flex>
            </ScrollView>
        );
    }
}
